const { Op } = require('sequelize');
const Resume = require('../models/resume');
const User = require('../models/User');

// 多条件查询简历
exports.getResumes = async (req, res) => {
  try {
    const { name, major, education, workExperience, skills } = req.query;
    const whereClause = {};

    // 构建查询条件
    if (name) whereClause.fullName = { [Op.like]: `%${name}%` };
    if (major) whereClause.major = { [Op.like]: `%${major}%` };
    if (education) whereClause.highestEducation = education;
    if (workExperience) whereClause.workExperience = { [Op.like]: `%${workExperience}%` };
    if (skills) whereClause.skills = { [Op.like]: `%${skills}%` };

    // 执行查询
    const resumes = await Resume.findAll({
      where: whereClause,
      include: [{
        model: User,
        attributes: ['id', 'email', 'role']
      }],
      order: [['updatedAt', 'DESC']]
    });

    res.status(200).json(resumes);
  } catch (error) {
    console.error('简历查询失败:', error);
    res.status(500).json({ message: '服务器错误，查询简历失败' });
  }
};

// 获取简历详情
exports.getResumeById = async (req, res) => {
  try {
    const resume = await Resume.findByPk(req.params.id, {
      include: [{
        model: User,
        attributes: ['id', 'email', 'role']
      }]
    });

    if (!resume) {
      return res.status(404).json({ message: '简历不存在' });
    }

    res.status(200).json(resume);
  } catch (error) {
    console.error('获取简历详情失败:', error);
    res.status(500).json({ message: '服务器错误，获取简历详情失败' });
  }
};